//
//  TGSearchResultController.m
//  GroupBuy
//
//  Created by wwj on 15/11/3.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGSearchResultController.h"
#import "TGDataTool.h"
#import "PinYin4Objc.h"
#import "TGCityModel.h"
#import "TGCitySectionModel.h"

@interface TGSearchResultController ()
@property (strong, nonatomic) NSMutableArray *resultCitiyArray;
@end

@implementation TGSearchResultController

-(NSMutableArray *)resultCitiyArray{
    if (_resultCitiyArray == nil) {
        _resultCitiyArray = [NSMutableArray array];
    }
    return _resultCitiyArray;
}

-(void)setSearchText:(NSString *)searchText{
    _searchText = searchText;
    [_resultCitiyArray removeAllObjects];
    
    HanyuPinyinOutputFormat *fmt = [[HanyuPinyinOutputFormat alloc] init];
    fmt.caseType = CaseTypeUppercase;
    fmt.toneType = ToneTypeWithoutTone;
    fmt.vCharType = VCharTypeWithUUnicode;
    
    NSDictionary *cities = [TGDataTool shareDataTool].dictCitySection;
    [cities enumerateKeysAndObjectsUsingBlock:^(NSString *key, TGCityModel *obj, BOOL *stop) {
        // SHI#JIA#ZHUANG
        // 1.拼音
        NSString *pinyin = [PinyinHelper toHanyuPinyinStringWithNSString:obj.name withHanyuPinyinOutputFormat:fmt withNSString:@"#"];
        
        // 2.拼音首字母
        NSArray *words = [pinyin componentsSeparatedByString:@"#"];
        NSMutableString *pinyinHeader = [NSMutableString string];
        for (NSString *word in words) {
            [pinyinHeader appendString:[word substringToIndex:1]];
        }
        
        /*
         补充：这里少加一行代码
         */
        pinyin = [pinyin stringByReplacingOccurrencesOfString:@"#" withString:@""];
        
        // 3.城市名中包含了搜索条件
        // 拼音中包含了搜索条件
        // 拼音首字母中包含了搜索条件
        if (([obj.name rangeOfString:searchText].length != 0) ||
            ([pinyin rangeOfString:searchText.uppercaseString].length != 0)||
            ([pinyinHeader rangeOfString:searchText.uppercaseString].length != 0)){
            // 说明城市名中包含了搜索条件
            [self.resultCitiyArray addObject:obj];
        }
    }];
    
    // 3.刷新表格
    [self.tableView reloadData];
}

-(void)viewDidLoad{
    [super viewDidLoad];
}

#pragma -mark tableView
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [NSString stringWithFormat:@"共%zd个搜索结果", _resultCitiyArray.count];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _resultCitiyArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifID];
    }
    TGCityModel *city = _resultCitiyArray[indexPath.row];
    cell.textLabel.text = city.name;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [TGDataTool shareDataTool].currentCity = _resultCitiyArray[indexPath.row];
}
@end
